<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>购买</title>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="validator" uri="http://www.springmodules.org/tags/commons-validator" %>
<core:set var ="webcontext" scope="request" value="${pageContext.request.contextPath}"/>
<core:set var ="imagepath" scope="request" value="${webcontext}/images"/>
<core:set var ="csspath" scope="request" value="${webcontext}/css"/>
<core:set var ="jspath" scope="request" value="${webcontext}/javascript"/>
<core:set var ="jsppath" scope="request" value="${webcontext}/jsp"/>
<core:set var ="htmlpath" scope="request" value="${webcontext}/html"/>
<core:set var ="bootstrapPath" scope="request" value="${webcontext}/bootstrap"/>
<core:set var ="layerPath" scope="request" value="${webcontext}/layer"/>
<!-- TODO the gdms url should replace by the token -->
<core:set var ="gdmsurl" scope="request" value="@token-gdms.server.servlet.context.url.integration@"/>
<script type="text/javascript">
	var contextPath = "${webcontext}";
</script>
<%@page import="com.cap.bts.framework.common.util.Pager"%>
<%@page import="com.cap.bts.framework.common.constant.CommonConstants"%>
<%@page import="com.cap.bts.framework.common.util.CodeTextUtils"%>
<%@page import="com.cap.bts.framework.common.util.CookieUtils"%>
<%@page import="com.cap.bts.framework.auth.constant.AuthConstant"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<link rel="stylesheet" type="text/css" href="${csspath }/style_l.css" />
<script type="text/javascript" src="${jspath }/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${layerPath}/layer.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.alerts.js"></script>
<link rel="stylesheet" type="text/css"	href="${csspath}/jquery.alerts.css" />
<script type="text/javascript" src="${jspath}/load_layer.js"></script>
<link rel="stylesheet" type="text/css"	href="${webcontext}/css/validationEngine.jquery.css" />
<script src="${jspath}/jquery/validate/jquery.validationEngine.js"	type="text/javascript"></script>
<script src="${jspath}/jquery/validate/jquery.validationEngine-zh.js" type="text/javascript"></script>
<%-- <link rel="stylesheet" type="text/css"	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script> --%>

<style type="text/css">
* {
	margin: 0px;
	padding: 0px;
	font-family: "Microsoft YaHei";
	color: #000;
}

body{min-width:600px;}

html,body {
	font: 12px "Microsoft YaHei", "Microsoft YaHei", Verdana, Arial,
		sans-serif;
	color: #000;
	overflow:auto;
}
.form-label{text-align: right; padding-right: 15px; width:200px;}
.input-price{height:25px;width:300px;}
select{height:25px;}

</style>
<script type="text/javascript">
$(function(){
	$('#packageNameSel').change(function(){
		//选择是请选择时候直接返回
		if($("#packageNameSel").val()==-1){
			$("#packageNumberTxt").val("");
			$("#price").html("");
			$("#quantity").html("");
			return;
		}
	    //按选择套餐设置隐藏域
		$("#price").html($("#price"+$("#packageNameSel").val()+"Htxt").val()+"元");
		$("#priceHtxt").val($("#price"+$("#packageNameSel").val()+"Htxt").val());
		$("#isPhysicalCardHtxt").val($("#isPhysicalCard"+$("#packageNameSel").val()+"Htxt").val());
		$("#isPrepaymentCardHtxt").val($("#isPrepaymentCard"+$("#packageNameSel").val()+"Htxt").val());
		$("#unitOfPeriodHtxt").val($("#unitOfPeriod"+$("#packageNameSel").val()+"Htxt").val());
		$("#validPeriodHtxt").val($("#validPeriod"+$("#packageNameSel").val()+"Htxt").val());
		//设置卡片名称
		$("#cardName").html($("#cardDefinitionName"+$("#packageNameSel").val()+"Htxt").val());
		$("#cardDefinitionIdHtxt").val($("#cardDefinitionId"+$("#packageNameSel").val()+"Htxt").val());
		//按实物卡非实物卡设置卡号和仅购买按钮
		if($("#isPhysicalCard"+$("#packageNameSel").val()+"Htxt").val()==0){
			$("#packageNumber1Div").css('display',''); 
			$("#packageNumber2Div").css('display','none');
			$("#cativeAfter").attr('disabled','disabled');
			$("#cativeAfter").css('color','#ebebeb');
			$("#cativeAfter").css('border-color','#ebebeb');
		}else if($("#isPhysicalCard"+$("#packageNameSel").val()+"Htxt").val()==1){
			$("#packageNumber1Div").css('display','none'); 
			$("#packageNumber2Div").css('display','');
			$("#cativeAfter").attr('disabled',false);
			$("#cativeAfter").css('color','#000');
			$("#cativeAfter").css('border-color','#ADA9A4');
		}
		
		//获取库存
		$.ajax({
			type:'POST',
			url: '${webcontext}/MARS/getPackageInfoMars',
			async: false,
			data : "cardDefId="+$("#cardDefinitionId"+$("#packageNameSel").val()+"Htxt").val()+"&dealerId="+$("#dealerIdHtxt").val(),
			dataType : 'json',
			success : function(data) {
				$("#quantity").html(data);
				$("#onhandQuantityHtxt").val(data);
		    }
		}); 
	}); 

	//取消
	$("#cancel").click(function(){
    	jAlert("请直接关闭当前窗口！","提示信息");
	});
	
	//购买并激活
	$("#activeNow").click(function(){
		
		//套餐选择为空时
		if($('#packageNameSel').val()=='-1'){
			$("#msg").html("请选择套餐类型！");
			//jAlert("","提示信息");
			return;
		}
		
		if($("#onhandQuantityHtxt").val()=='0'){
			$("#msg").html("库存不足！");
			//jAlert("库存不足","提示信息");
			return;
		}
		
 			//判断是否存在该未销售实物卡
 			if($("#isPhysicalCard"+$("#packageNameSel").val()+"Htxt").val()==1){
 				//套餐选择为空时
 				if($('#packageNumberTxt').val()==''){
 					$("#msg").html("卡号不能为空！");
 					//jAlert("卡号不能为空！","提示信息");
 					return;
 				}
 				$("#msg").html("<img src='${imagepath}/loading2.gif' />交易处理中...");
 				$("#activeNow").toggle();
  			$.ajax({
  				type:'POST',
  				url: '${webcontext}/MARS/checkMemberPackageInfoExsitMars',
  				async: false,
  				/* beforeSend: function(){ 
  					layer.load(1);
  				}, */
  				data : "packageDefId="+$("#packageNameSel").val()+"&packageNumber="+$("#packageNumberTxt").val(),
  				dataType : 'json',
  				success : function(data) {
  					/* layer.closeAll('loading'); */
  					if(data!=true){
  						$("#msg").html("输入卡号不存在或已经出售，请检查后重新输入");
  						$("#activeNow").toggle();
  						//jAlert("输入卡号不存在或已经出售，请检查后重新输入","提示信息");
  						return;
  					}else{
  						$.ajax({
  			  				type:'POST',
  			  				url: '${webcontext}/MARS/purchaseMars',
  			  				async: true,
  			  				data : $("form").serialize(),
  			  				dataType : 'json',
  			  			/* beforeSend: function(){ 
  		  					layer.load(1);
  		  				}, */
  			  				success : function(data) {
  			  				/* layer.closeAll('loading'); */
  			  				if(data.result==true){
			  						//jAlert("购买成功","提示信息");
			  						$("#msg").html("购买成功，请关闭当前窗口！");
			  						
			  					}else{
			  						$("#msg").html(data.errorMsg);
			  						$("#activeNow").toggle();
			  					}
  			  			    }
  			 				});
  					}
  			    },error:function(){
  			    	$("#msg").html("操作失败，请重新打开页面后重试！");
  			    }
 				}); 
 			}else{
 				$("#msg").html("<img src='${imagepath}/loading2.gif' />交易处理中...");
 				$("#activeNow").toggle();
 				$.ajax({
  				type:'POST',
  				url: '${webcontext}/MARS/purchaseMars',
  				async: true,
  				data : $("form").serialize(),
  				dataType : 'json',
  				success : function(data) {
  					if(data.result==true){
  						$("#msg").html("购买成功，请关闭当前窗口！");
  					}else{
  						$("#msg").html(data.errorMsg);
  						$("#activeNow").toggle();
  					}
	  			},error:function(){
	  			    	$("#msg").html("操作失败，请重新打开页面后重试！");
	  			    }
 				});
 			}
});
	
});

$(function(){
	$("#packageNumberTxt").on("keypress", function(event){
		if(13 == event.keyCode){
			return false;
		}
		
	});
});
</script>

</head>
<body>

			<form:form id="edit_purchase" action="#" method="post"
				modelAttribute="submitDto" enctype="multipart/form-data">
				<div class="data_div">
					<table class="data_tab">
						<tr>
							<th class="form-label">手机</th>
							<td>
							<input type="hidden" id="dealerIdHtxt"
								name="dealerId" value="${memberResultsDto.dealerId}">
							<input type="hidden" id="memberNameHtxt"
								name="memberId" value="${memberResultsDto.id}">
							<input type="hidden" id="memberNameHtxt"
								name="memberName" value="${memberResultsDto.memberName}">
								<input type="hidden" id="mobilePhoneHtxt" name="mobilePhone"
								value="${memberResultsDto.mobilePhone}"> <input
								type="hidden" id="carPlateTHtxt" name="carPlate"
								value="${memberResultsDto.carPlate}"> <input
								type="hidden" id="priceHtxt" name="price"> <input
								type="hidden" id="isPhysicalCardHtxt" name="isPhysicalCard" /> <input
								type="hidden" id="isPrepaymentCardHtxt" name="isPrepaymentCard">
								<input type="hidden" id="unitOfPeriodHtxt" name="unitOfPeriod">
								<input type="hidden" id="validPeriodHtxt" name="validPeriod">
								<input type="hidden" id="onhandQuantityHtxt"
								name="onhandQuantity"> <input type="hidden"
								id="cardDefinitionIdHtxt" name="cardDefinition.id">
								${memberResultsDto.mobilePhone}</td>
						</tr>
						<tr>
							<th class="form-label">姓名</th>
							<td>${memberResultsDto.memberName}</td>
						</tr>
						<tr>
							<th class="form-label">车牌</th>
							<td>${memberResultsDto.carPlate}</td>
						</tr>
						<tr>
							<th class="form-label">订单编号</th>
							<td><input type="hidden" id="soNumberHtxt"
								name="soNumberSale" value="${memberResultsDto.soNumber}">${memberResultsDto.soNumber}</td>
						</tr>
						<tr>
							<th class="form-label">套餐类型</th>
							<td>
							<core:if test="${memberResultsDto.carPlate!=null || memberResultsDto.carPlate!=''}" >
							<core:forEach items="${packageDefinitionListDto}"
									var="listH">
									<input type="hidden" id="price${listH.id}Htxt"
										value="${listH.price}" />
									<input type="hidden" id="isPhysicalCard${listH.id}Htxt"
										value="${listH.isPhysicalCard}" />
									<input type="hidden" id="isPrepaymentCard${listH.id}Htxt"
										value="${listH.isPrepaymentCard}" />
									<input type="hidden" id="unitOfPeriod${listH.id}Htxt"
										value="${listH.unitOfPeriod}" />
									<input type="hidden" id="validPeriod${listH.id}Htxt"
										value="${listH.validPeriod}" />
								</core:forEach> <core:forEach items="${packageDefinitionListDto}"
									var="listCardH">
									<input type="hidden" id="cardDefinitionId${listCardH.id}Htxt"
										value="${listCardH.cardDefinition.id}" />
									<input type="hidden" id="cardDefinitionName${listCardH.id}Htxt"
										value="${listCardH.cardDefinition.name}" />
								</core:forEach> <select id="packageNameSel" class="form-control"
								name="packageDefId" style="width: 300px">
									<core:forEach items="${packageDefinitionListDto}" var="list">
										<core:choose>
											<core:when test='${list.id=="-1"  }'>
												<option value="${list.id}" selected>请选择套餐</option>
											</core:when>
											<core:when test='${!empty memberResultsDto.carPlate}'>
												<option value="${list.id}">${list.name}</option>
											</core:when>
											<core:when test='${empty memberResultsDto.carPlate}'>
											<core:if test='${ list.isPhysicalCard==1}'>
													<option value="${list.id}">${list.name}</option>
												</core:if>
											</core:when>
										</core:choose>
									</core:forEach>
							</select>
								</core:if>
							</td>
						</tr>
						<tr>
							<th class="form-label">套餐卡名称</th>
							<td id="cardName"></td>
						</tr>
						<tr>
							<th class="form-label">库存</th>
							<td id="quantity"></td>
						</tr>

						<tr>
							<th class="form-label">卡号</th>
							<td>
								<div id="packageNumber1Div" style="display: none">购买后自动生成</div>
								<div id="packageNumber2Div" style="width: 150px">
									<input id="packageNumberTxt" name="packageNumber" 
										class="form-control input-price" value="${memberResultsDto.packageNumber}">
								</div>
							</td>
						</tr>
						<tr>
							<th class="form-label">实际支付</th>
							<td id="price"></td>
						</tr>
						<tr>
							<th class="form-label"></th>
							<td><span id="msg" style="color: red;"></span></td>
						</tr>
					</table>
				</div>
				<div class="spliter h_5"></div>
				<div class="btn_div" style="width: 600px">
					<button type="button" class="btn" style="width: 80px; height: 30px"
						id="activeNow">确定</button>
					<!-- <button type="button" class="btn" style="width: 80px; height: 30px"
						id="cancel">取消</button> -->
				</div>
			</form:form>



</body>

</html>